package com.kaibes.platform.currency.api;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaibes.platform.currency.db.CurrencyExchange;
import com.kaibes.platform.currency.db.CurrencyExchangeService;
import com.kaibes.platform.currency.pojo.CurrencyExchangeUpdate;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ResponseApi;
import com.kaibes.web.response.ResponseFactory;

@RestController
@RequestMapping("/currencys/exchanges")
public class CurrencyExchangeApi extends ResponseApi<CurrencyExchange, CurrencyExchangeService> {

    @PostMapping
    @ApiLink("创建汇率")
    public ResponseEntity<List<Number>> post(@RequestBody @Validated CurrencyExchange data) {
        if (data.getFromId() == data.getToId()) {
            return ResponseFactory.postFailure("两种货币不能相同！");
        }
        return super.post(data, "创建汇率成功", "创建汇率失败");
    }
    
    @DeleteMapping
    @ApiLink("删除汇率，根据id")
    public ResponseEntity<Object> deleteByIds(@RequestBody List<Integer> ids) {
        return super.deleteById(ids, "删除汇率成功", "删除汇率失败");
    }
    
    @DeleteMapping("/{id:\\d+}")
    @ApiLink("删除汇率，根据id")
    public ResponseEntity<Object> deleteById(@PathVariable Integer id) {
        return super.deleteById(id, "删除汇率成功", "删除汇率失败");
    }
    
    @PutMapping("/{id:\\d+}")
    @ApiLink("修改汇率，根据id")
    public ResponseEntity<Object> putById(@PathVariable Integer id, @RequestBody CurrencyExchangeUpdate data) {
        return super.putById(data, id, "修改汇率成功", "修改汇率失败");
    }
    
}
